<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时间数据类型选择指南 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.6;
            color: #333;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
        }
        .highlight:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 40%;
            background-color: rgba(118, 75, 162, 0.2);
            z-index: -1;
        }
        .scroll-indicator {
            animation: bounce 2s infinite;
        }
        @keyframes bounce {
            0%, 20%, 50%, 80%, 100% {transform: translateY(0);}
            40% {transform: translateY(-10px);}
            60% {transform: translateY(-5px);}
        }
        .dropdown-content {
            transition: all 0.3s ease;
            max-height: 0;
            overflow: hidden;
        }
        .dropdown-content.active {
            max-height: 1000px;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <span class="bg-white text-purple-700 px-3 py-1 rounded-full text-sm font-medium mb-4">技术指南</span>
                <h1 class="text-4xl md:text-5xl font-bold mb-6">项目中的<span class="highlight">时间数据类型</span>选择指南</h1>
                <p class="text-xl md:text-2xl text-purple-100 mb-8 max-w-3xl">在系统开发中优雅地处理时间数据的存储与表示</p>
                <div class="flex space-x-4">
                    <a href="#database" class="bg-white text-purple-700 px-6 py-3 rounded-lg font-medium hover:bg-purple-50 transition duration-300">数据库存储</a>
                    <a href="#programming" class="bg-transparent border-2 border-white text-white px-6 py-3 rounded-lg font-medium hover:bg-white hover:text-purple-700 transition duration-300">编程语言</a>
                </div>
            </div>
        </div>
        <div class="text-center mt-16 scroll-indicator">
            <i class="fas fa-chevron-down text-white text-2xl"></i>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl py-12 px-4">
        <!-- Intro -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8">
                <p class="text-lg text-gray-700 mb-6">
                    在项目中存储下单时间（或其他时间戳）时，通常有几种常见的数据类型可以选择，具体选择取决于使用的数据库系统、编程语言及业务需求。本指南将详细介绍在不同场景下如何选择最合适的时间数据类型。
                </p>
                <div class="flex flex-wrap gap-4">
                    <div class="flex items-center bg-purple-50 text-purple-700 px-4 py-2 rounded-full">
                        <i class="fas fa-database mr-2"></i>
                        <span>数据库类型</span>
                    </div>
                    <div class="flex items-center bg-blue-50 text-blue-700 px-4 py-2 rounded-full">
                        <i class="fas fa-code mr-2"></i>
                        <span>编程语言</span>
                    </div>
                    <div class="flex items-center bg-green-50 text-green-700 px-4 py-2 rounded-full">
                        <i class="fas fa-clock mr-2"></i>
                        <span>时间精度</span>
                    </div>
                    <div class="flex items-center bg-yellow-50 text-yellow-700 px-4 py-2 rounded-full">
                        <i class="fas fa-globe mr-2"></i>
                        <span>时区处理</span>
                    </div>
                </div>
            </div>
        </section>

        <!-- Database Section -->
        <section id="database" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">1. 数据库中的时间存储</h2>
            
            <div class="mb-12">
                <h3 class="text-2xl font-semibold mb-6 text-gray-700 flex items-center">
                    <i class="fas fa-server text-purple-600 mr-3"></i>
                    <span>1.1 SQL 数据库</span>
                </h3>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <!-- DATETIME Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-purple-100 p-4">
                            <h4 class="text-xl font-bold text-purple-800">DATETIME</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>存储日期和时间，精确到秒（例如：<code>2024-08-15 12:34:56</code>）。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>适用于存储日期和时间的详细信息
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适合需要精确记录到秒的场景，如订单创建时间、用户注册时间等
                                </p>
                            </div>
                        </div>
                    </div>
                    
                    <!-- TIMESTAMP Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-blue-100 p-4">
                            <h4 class="text-xl font-bold text-blue-800">TIMESTAMP</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>通常也用于存储日期和时间，精确到秒。<code>TIMESTAMP</code> 数据类型通常会受到时区的影响。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>自动处理时区转换和默认值（如记录创建时间）
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适合需要与时区相关的时间记录
                                </p>
                            </div>
                        </div>
                    </div>
                    
                    <!-- DATE Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-green-100 p-4">
                            <h4 class="text-xl font-bold text-green-800">DATE</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>仅存储日期，不包括时间（例如：<code>2024-08-15</code>）。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>节省存储空间，适合只需要记录日期的场景
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适用于记录只需要日期的场景，例如出生日期
                                </p>
                            </div>
                        </div>
                    </div>
                    
                    <!-- TIME Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-yellow-100 p-4">
                            <h4 class="text-xl font-bold text-yellow-800">TIME</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>仅存储时间，不包括日期（例如：<code>12:34:56</code>）。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>适合只需要记录时间的场景
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>例如记录事件发生的具体时间（如订单处理时间）
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div>
                <h3 class="text-2xl font-semibold mb-6 text-gray-700 flex items-center">
                    <i class="fas fa-database text-blue-600 mr-3"></i>
                    <span>1.2 NoSQL 数据库</span>
                </h3>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <!-- ISO 8601 Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-indigo-100 p-4">
                            <h4 class="text-xl font-bold text-indigo-800">ISO 8601 格式的字符串</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>使用字符串格式存储时间，如 <code>2024-08-15T12:34:56Z</code>。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>与 JSON 兼容，易于存储和检索
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适用于存储和交换时间数据的场景，特别是在 JSON 格式的 NoSQL 数据库中
                                </p>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Epoch Time Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-pink-100 p-4">
                            <h4 class="text-xl font-bold text-pink-800">时间戳（Epoch Time）</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>存储自 Unix 纪元（1970年1月1日）以来的秒数或毫秒数。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>存储和计算时间的方式高效且简洁
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适合需要高效存储和比较时间的场景
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Programming Section -->
        <section id="programming" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">2. 编程语言中的时间存储</h2>
            
            <div class="mb-12">
                <h3 class="text-2xl font-semibold mb-6 text-gray-700 flex items-center">
                    <i class="fab fa-java text-red-600 mr-3"></i>
                    <span>2.1 Java</span>
                </h3>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <!-- LocalDateTime Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-red-100 p-4">
                            <h4 class="text-xl font-bold text-red-800">java.time.LocalDateTime</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>存储本地日期和时间（不包括时区）。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>适用于需要存储和操作日期和时间的场景
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适合记录订单创建时间等需要本地时间的场景
                                </p>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Instant Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-orange-100 p-4">
                            <h4 class="text-xl font-bold text-orange-800">java.time.Instant</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>表示自 Unix 纪元以来的时间点。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>适合高精度时间戳
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适合存储精确的时间戳，如订单创建的精确时间
                                </p>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Date & Calendar Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-amber-100 p-4">
                            <h4 class="text-xl font-bold text-amber-800">java.util.Date/Calendar</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>旧版的日期和时间 API，用于表示日期和时间。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>与旧系统兼容
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适用于需要与旧代码兼容的场景，但推荐使用新的 <code>java.time</code> API
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div>
                <h3 class="text-2xl font-semibold mb-6 text-gray-700 flex items-center">
                    <i class="fab fa-python text-blue-500 mr-3"></i>
                    <span>2.2 Python</span>
                </h3>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <!-- datetime Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-blue-100 p-4">
                            <h4 class="text-xl font-bold text-blue-800">datetime.datetime</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>用于存储日期和时间。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>灵活且功能丰富
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适用于记录和操作日期时间的场景
                                </p>
                            </div>
                        </div>
                    </div>
                    
                    <!-- time.time() Card -->
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition duration-300">
                        <div class="bg-green-100 p-4">
                            <h4 class="text-xl font-bold text-green-800">time.time()</h4>
                        </div>
                        <div class="p-6">
                            <p class="text-gray-700 mb-4">
                                <strong>描述：</strong>返回自 Unix 纪元以来的秒数（浮点数）。
                            </p>
                            <div class="bg-gray-50 p-3 rounded-lg mb-4">
                                <p class="text-green-600 font-medium">
                                    <i class="fas fa-check-circle mr-2"></i>
                                    <span>优点：</span>适合计算时间间隔
                                </p>
                            </div>
                            <div class="bg-gray-50 p-3 rounded-lg">
                                <p class="text-blue-600 font-medium">
                                    <i class="fas fa-lightbulb mr-2"></i>
                                    <span>使用场景：</span>适合高精度时间戳和时间差计算
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Decision Tree Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">时间数据类型选择流程图</h2>
            <div class="bg-white rounded-xl shadow-lg p-6">
                <div class="mermaid">
                    graph TD
                    A[开始] --> B{需要存储时间数据?}
                    B -->|是| C{使用哪种数据库?}
                    B -->|否| D[结束]
                    C -->|SQL| E{需要精确到秒?}
                    C -->|NoSQL| F{需要高效存储?}
                    E -->|是| G{需要时区支持?}
                    E -->|否| H[使用 DATE 或 TIME]
                    G -->|是| I[使用 TIMESTAMP]
                    G -->|否| J[使用 DATETIME]
                    F -->|是| K[使用 Epoch Time]
                    F -->|否| L[使用 ISO 8601 字符串]
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">最佳实践总结</h2>
            <div class="bg-gradient-to-r from-purple-50 to-blue-50 rounded-xl shadow-lg p-8">
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-purple-700 flex items-center">
                            <i class="fas fa-check-circle mr-2"></i>
                            <span>SQL 数据库推荐</span>
                        </h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 flex-shrink-0">1</span>
                                <span>优先使用 <code>TIMESTAMP</code> 如果需要自动时区转换</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 flex-shrink-0">2</span>
                                <span>使用 <code>DATETIME</code> 如果不需要时区转换</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 flex-shrink-0">3</span>
                                <span>对于仅需日期或时间的场景，使用专门的 <code>DATE</code> 或 <code>TIME</code> 类型</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-blue-700 flex items-center">
                            <i class="fas fa-check-circle mr-2"></i>
                            <span>NoSQL/编程语言推荐</span>
                        </h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <span class="bg-blue-100 text-blue-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 flex-shrink-0">1</span>
                                <span>优先使用 ISO 8601 格式字符串，便于 JSON 兼容</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-blue-100 text-blue-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 flex-shrink-0">2</span>
                                <span>对于高性能场景，考虑使用 Unix 时间戳</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-blue-100 text-blue-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 flex-shrink-0">3</span>
                                <span>在应用中统一时区处理策略</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-lg font-medium text-white">技术小馆</h3>
                    <p class="text-gray-400">专业的技术知识分享平台</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300 flex items-center">
                        <i class="fas fa-globe mr-2"></i>
                        <span>技术小馆地址：http://www.yuque.com/jtostring</span>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-sm text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });

        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });

        // Dropdown functionality
        document.querySelectorAll('.dropdown-toggle').forEach(toggle => {
            toggle.addEventListener('click', function() {
                const content = this.nextElementSibling;
                content.classList.toggle('active');
            });
        });
    </script>
</body>
</html>